package com.sg.hms.DaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mysql.jdbc.Util;
import com.sg.hms.Dao.FoodMenuDao;
import com.sg.hms.Model.FoodMenu;
import com.sg.hms.Util.JDBCUtil;

public class FoodMenuDaoImpl implements FoodMenuDao {

	@Override
	public List<FoodMenu> getFoods(int start, int pageSize, int uid) {
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer(
				"select * from foodmenu where elId=?");
		sql.append(" limit ?,?");
		List<FoodMenu> foodMenus = new ArrayList<FoodMenu>();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, uid);
			pst.setInt(2, start);
			pst.setInt(3, pageSize);
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				FoodMenu fm = new FoodMenu();
				fm.setFmid(rs.getInt("fmId"));
				fm.setElId(rs.getInt("elId"));
				fm.setFmName(rs.getString("fmName"));
				fm.setPrice(rs.getDouble("price"));
				fm.setContent(rs.getString("content"));
				foodMenus.add(fm);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return foodMenus;
	}

	@Override
	public List<FoodMenu> geFoods(int start, int pageSize, int uid,
			String searchname) {
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from foodmenu where elId=?");
		sql.append(" and fmName like ?");
		sql.append(" limit ?,?");
		List<FoodMenu> foodMenus = new ArrayList<FoodMenu>();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, uid);
			pst.setString(2, "%" + searchname + "%");
			pst.setInt(3, start);
			pst.setInt(4, pageSize);
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				FoodMenu fm = new FoodMenu();
				fm.setFmid(rs.getInt("fmId"));
				fm.setElId(rs.getInt("elId"));
				fm.setFmName(rs.getString("fmName"));
				fm.setPrice(rs.getDouble("price"));
				fm.setContent(rs.getString("content"));
				foodMenus.add(fm);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return foodMenus;
	}

	@Override
	public int getFoodMenusCounts(int elId) {
		int fcounts = 0;
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer(
				"SELECT COUNT(*) fcounts FROM foodmenu where elId=?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, elId);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				fcounts = rs.getInt("fcounts");
			}
			// System.out.println(fcounts);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return fcounts;
	}

	@Override
	public int addFoodMenu(int elID,String fmName,double price,String content) {
		
		int flag=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("insert into foodmenu(elID,fmName,price,content) values(?,?,?,?)");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setInt(1, elID);
			pst.setString(2, fmName);
			pst.setDouble(3, price);
			pst.setString(4, content);
			int a=pst.executeUpdate();
			if (a>0) {				
				flag=1;
				}
			}catch (SQLException e){
					e.printStackTrace();
					}
		
					return flag;
			}

	@Override
	public int deleteFoodMenus(int fmId) {
		Connection con=JDBCUtil.getConnection();
		int flag=0;
		StringBuffer sql=new StringBuffer("delete  from foodmenu where fmId=?");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setInt(1, fmId);
			int a=pst.executeUpdate();
			if (a>0) {
				flag=1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}

	@Override
	public int updateFoodMenus(int fmid, String fmName, int price,
			String content) {
		Connection con=JDBCUtil.getConnection();
		int flag=0;
		StringBuffer sql=new StringBuffer("update foodmenu set fmName=?,price=?,content=? where fmId=?");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setString(1, fmName);
			pst.setInt(2, price);
			pst.setString(3, content);
			pst.setInt(4, fmid);
			int a=pst.executeUpdate();
			if (a>0) {
				flag=1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
 }

	@Override
	public List<FoodMenu> getFoods(int start,int pageSize){
			Connection con = JDBCUtil.getConnection();
			StringBuffer sql = new StringBuffer("select * from foodmenu");
			sql.append(" limit ?,?");
			List<FoodMenu> foodMenus = new ArrayList<FoodMenu>();
			try {
				PreparedStatement pst = con.prepareStatement(sql.toString());
				pst.setInt(1, start);
				pst.setInt(2, pageSize);
				ResultSet rs = pst.executeQuery();
				while (rs.next()) {
					FoodMenu fm = new FoodMenu();
					fm.setFmid(rs.getInt("fmId"));
					fm.setElId(rs.getInt("elId"));
					fm.setFmName(rs.getString("fmName"));
					fm.setPrice(rs.getDouble("price"));
					fm.setContent(rs.getString("content"));
					foodMenus.add(fm);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return foodMenus;
	}

	@Override
	public int getFoodMenusCounts() {
		int fcounts = 0;
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer(
				"SELECT COUNT(fmID) fcounts FROM foodmenu");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				fcounts = rs.getInt("fcounts");
			}
			// System.out.println(fcounts);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return fcounts;
}
}